package com.pys.service;

import java.util.List;

import org.hibernate.Session;

import com.pys.dao.impl.ProjeDAO;
import com.pys.pojo.PysProje;
import com.pys.utils.HibernateUtil;

public class ProjeService {

	public PysProje getById(long id) {
		Session session = null;
		PysProje pysProje = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			ProjeDAO projeDAO = new ProjeDAO(session);
			pysProje = (PysProje) projeDAO.findById(PysProje.class, id);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return pysProje;
	}

	public void save(PysProje pysProje) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			ProjeDAO projeDAO = new ProjeDAO(session);
			projeDAO.save(pysProje);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public void update(PysProje pysProje) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			ProjeDAO projeDAO = new ProjeDAO(session);
			projeDAO.update(pysProje);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	// public void roleEkle(String roleAd) {
	// Session session = HibernateUtil.getSessionFactory().openSession();
	// session.beginTransaction();
	// ProjeDAO projeDAO = new ProjeDAOImpl(session);
	// PysProje pysProje = new PysProje();
	// pysProje.setAd(roleAd);
	// projeDAO.save(pysProje);
	// session.getTransaction().commit();
	// session.close();
	// }

	public List<PysProje> getAllList(Boolean deleted) {
		Session session = null;
		List<PysProje> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			ProjeDAO projeDAO = new ProjeDAO(session);
			if (deleted == null)
				list = projeDAO.findAll(PysProje.class);
			else
				list = projeDAO.findByProperty(PysProje.class, "deleted",
						deleted);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

	public void delete(PysProje pysProje) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			ProjeDAO projeDAO = new ProjeDAO(session);
			projeDAO.delete(pysProje);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public List<PysProje> getByName(String name) {
		Session session = null;
		List<PysProje> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			ProjeDAO projeDAO = new ProjeDAO(session);
			list = projeDAO.findByProperty(PysProje.class, "name", name);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

	public List<PysProje> getAllAktifList() {
		Session session = null;
		List<PysProje> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			ProjeDAO projeDAO = new ProjeDAO(session);
			list = projeDAO.findByProperties(PysProje.class, new String[] {
					"deleted", "durum" }, new Object[] { false, true });
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

}
